package com.xyz.bigcorn.admin.common.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.*;

import javax.servlet.MultipartConfigElement;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangl on 2017/11/21.
 * todo:springMVC配置文件
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    //访问静态资源
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static*/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/view*/**").addResourceLocations("classpath:/view/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/welcome").setViewName("home/welcome");
        registry.addViewController("/form-basic").setViewName("template/form/form-basic");
        registry.addViewController("/form-advance").setViewName("template/form/form-advance");
        registry.addViewController("/form-step").setViewName("template/form/form-step");
        registry.addViewController("/table-basic").setViewName("template/table/table-basic");
        registry.addViewController("/table-advance").setViewName("template/table/table-advance");
        registry.addViewController("/table-tree").setViewName("template/table/table-tree");
        registry.addViewController("/table-ltrt").setViewName("template/table/table-ltrt");
        registry.addViewController("/table-img").setViewName("template/table/table-img");
        registry.addViewController("/error-500").setViewName("template/error/error-500");
        registry.addViewController("/error-404").setViewName("template/error/error-404");
        registry.addViewController("/error-303").setViewName("template/error/error-303");
        registry.addViewController("/user-info").setViewName("template/user-info");
        registry.addViewController("/fileMain").setViewName("template/file/fileMain");
        registry.addViewController("/empty").setViewName("page/other/empty");
        registry.addViewController("/side-more").setViewName("other/side-more");
        registry.addViewController("/dropdown").setViewName("plugin/dropdown");
        registry.addViewController("/notice").setViewName("plugin/notice");
        registry.addViewController("/dialog").setViewName("plugin/dialog");
        registry.addViewController("/other").setViewName("plugin/other");
    }
    @Bean
    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
        registration.setMultipartConfig(multipartConfigElement());
        dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
        return registration;
    }

    /**
     *  fastjson序列化
     *
     * */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");    // 自定义时间格式
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.BrowserCompatible, SerializerFeature.WriteNonStringKeyAsString);
        converters.add(fastJsonHttpMessageConverter);
        converters.add(responseBodyConverter());
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 设置文件大小限制 ,超出设置页面会抛出异常信息，
        // 这样在文件上传的地方就需要进行异常信息的处理了;
        factory.setMaxFileSize("10MB"); // KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize("50MB");
        // Sets the directory location where files will be stored.
        // factory.setLocation("路径地址");
        return factory.createMultipartConfig();
    }

    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(
                Charset.forName("UTF-8"));
        return converter;
    }


}
